package com.sinoglobal.sinostore.activity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.LogUtils;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.StateBaseActivity;
import com.sinoglobal.sinostore.bean.BaseVo;
import com.sinoglobal.sinostore.bean.OrderListVo;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.ApiDebugUtil;
import com.sinoglobal.sinostore.utils.BitmapHelp;
import com.sinoglobal.sinostore.utils.TextUtil;

/**
 * 
* @title: AddCommentActivity.java 
* @package com.chiliring.sinostore.activity 
* @description: TODO(发表评价) 
* @author LK
* @date 2015-1-27 下午5:51:32
 */
public class AddCommentActivity extends StateBaseActivity {

	public static final String ID="id";
	public static final String ORDER_NUM="ordernum";
	public static final String TYPE="type";
	public static final String NAME="name";
	public static final String IMG_URL="imgUrl";
	public static final String CASH_PAY="cash";
	public static final String PRICE_PAY="price";
	TextView tvGoodsName;
	TextView tvGoodsPrice;
	
	ImageView ivPic;
	
	EditText etComment;
	TextView tvWordNum;


	String id;//商品id
	String orderNum;//订单号
	String type;//评论类型：1=售后评论，2=售前咨询
	String name,imgUrl,cashPay,pricePay;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_activity_add_comment);
		openChenJInShi();
		tvGoodsName=getViewById(R.id.shop_goodsName);
		tvGoodsPrice=getViewById(R.id.shop_goodsPrice);
		ivPic=getViewById(R.id.shop_ivPic);
		etComment=getViewById(R.id.shop_etComment);
		tvWordNum=getViewById(R.id.shop_tvWordNum);
		
		titleView.setText("发表评价");
		
		id=getIntent().getStringExtra(ID);
		orderNum=getIntent().getStringExtra(ORDER_NUM);
		type=getIntent().getStringExtra(TYPE);
		name=getIntent().getStringExtra(NAME);
		imgUrl=getIntent().getStringExtra(IMG_URL);//图片的全路径
		cashPay=getIntent().getStringExtra(CASH_PAY);
		pricePay=getIntent().getStringExtra(PRICE_PAY);

		BitmapUtils bitmapUtils=BitmapHelp.getBitmapUtils(this);
		bitmapUtils.display(ivPic, imgUrl);

		tvGoodsName.setText(name);
		if(TextUtil.isZeroOrNull(pricePay) && !TextUtil.isZeroOrNull(cashPay)){//积分单价为零，现金不为零，为现金支付
			tvGoodsPrice.setText(String.format("单价：%s元", cashPay));
		}else if(TextUtil.isZeroOrNull(cashPay) && !TextUtil.isZeroOrNull(pricePay)){//现金单价为零，为积分支付
			tvGoodsPrice.setText(String.format("单价：%s%s",pricePay,getString(R.string.score_unit)));
		}else{//两个都不为零则为现金加积分支付
			tvGoodsPrice.setText(String.format("单价：%s元+%s%s", cashPay,pricePay,getString(R.string.score_unit)));
		}
		titleRightText.setText("提交");
		titleRightText.setTextSize(18);
		titleRightText.setTextColor(getResources().getColor(R.color.title_bg_red));
		baseButRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				subMitComment();
			}
		});
		
		etComment.addTextChangedListener(new TextWatcher() {
			
			@SuppressLint("NewApi")
			@Override
			public void onTextChanged(CharSequence charSequence, int arg1, int arg2, int arg3) {
				int num = 50 - charSequence.length();
				tvWordNum.setText(String.valueOf(num));
				if(num < 0){
					titleRightText.setTextColor(getResources().getColor(R.color.text_light_gray));
					baseButRight.setClickable(false);
				}else{
					baseButRight.setClickable(true);
					titleRightText.setTextColor(getResources().getColor(R.color.title_bg_red));
				}
			}
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			}
			@Override
			public void afterTextChanged(Editable arg0) {
			}
		});
	}
	/**
	 * 
	* @author LK
	* @methods subMitComment 
	* @description 描述信息  发表评价
	* @date 2015-1-27 下午5:50:51 参数说明
	 */
	private void subMitComment() {
		String comment = etComment.getText().toString().trim();
		if (TextUtils.isEmpty(comment)) {
			showShortToast("亲，您还没有输入评论内容哦");
			return;
		}
		HttpUtils httpUtils=new HttpUtils();
		final ProgressDialog progressDialog=new ProgressDialog(this);
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por", "105");
		params.addQueryStringParameter("proIden", Constants.STOREFLAG);
		params.addQueryStringParameter("user_id", Constants.userCenterId);
		params.addQueryStringParameter("message",comment);
		params.addQueryStringParameter("user_name",Constants.userName);
		params.addQueryStringParameter("nike_name",Constants.nickName);
		params.addQueryStringParameter("type",type);
		params.addQueryStringParameter("id",id);
		params.addQueryStringParameter("order_num",orderNum);
		ApiDebugUtil.debug(params);
		httpUtils.send(HttpRequest.HttpMethod.POST,Constants.STORE_URL,params,new RequestCallBack<String>() {
			@Override
			public void onStart() {
				try {
					progressDialog.show();
				} catch (Exception e) {
				}
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
				if (responseInfo!=null&&TextUtil.stringIsNotNull(responseInfo.result)) {
					LogUtils.e("responseInfo.result==>"+responseInfo.result);
					BaseVo baseVo=JSON.parseObject(responseInfo.result, OrderListVo.class);
					if (baseVo!=null) {
						if (Constants.STORE_SUCCESS_CODE.equals(baseVo.getCode())) {
							showShortToast("评价已发送，感谢您的参与~");
							new Handler().postDelayed(new Runnable() {
								@Override
								public void run() {
									setResult(RESULT_OK);
									AddCommentActivity.this.finish();
								}
							}, 2000);
						}
					}
				}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				try {
					progressDialog.dismiss();
				} catch (Exception e) {
				}
			}
		});
	}
}
